﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class SertificateSpeciality : IEquatable<SertificateSpeciality>
    {
        public void ImportList(List<Classes.SertificateSpeciality> sertSpecList)
        {
            var mdb = new MedregDBDataContext();
            var ListSertSpec = new List<SertificateSpeciality>();
            foreach (var sertSp in sertSpecList)
            {
                SertificateSpeciality sertSp1 = new SertificateSpeciality() { ID = sertSp.ID, Name = sertSp.Name, Parent = sertSp.Parent};
                SertificateSpeciality query;

                if (mdb.SertificateSpeciality.LongCount() == 0)
                {
                    mdb.SertificateSpeciality.InsertOnSubmit(sertSp1);
                }
                else
                {
                    try
                    {
                        query = mdb.SertificateSpeciality.Single(l => l.ID == sertSp1.ID);
                        query.Name = sertSp1.Name;

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {

                        if (!ListSertSpec.Contains(sertSp1))
                        {
                            mdb.SertificateSpeciality.InsertOnSubmit(sertSp1);
                            ListSertSpec.Add(sertSp1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(SertificateSpeciality other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
